package com.liran.controller;

import cn.hutool.core.util.RandomUtil;
import com.liran.mpPojo.SysUser;
import com.liran.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * @author: whh
 * @Date: 2022/7/9
 * @Description:
 */
@RestController
@RequestMapping("/sys/login")
public class LoginController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private StringRedisTemplate redisTemplate;


    @PostMapping("/confirm")
    public String confirm(@RequestBody SysUser sysUser) {
        if (sysUserService.confirm(sysUser)) {
            String token = RandomUtil.randomString(16);
            redisTemplate.opsForValue().set("web:login:token", token, 1, TimeUnit.HOURS);
            return token;
        }
        return null;
    }

    @GetMapping("/check/{token}")
    public Boolean check(@PathVariable String token){
        String existToken = redisTemplate.opsForValue().get("web:login:token");
        return existToken != null && existToken.equals(token);
    }
}
